#!/usr/bin/env tclsh
source streamtestlib.tcl

# Define records, protocol and startup (text goes to files)
# The asynPort "device" is connected to a network TCP socket
# Talk to the socket with send/receive/assure
# Send commands to the ioc shell with ioccmd

set records {
    record (longout, "DZ:test1")
    {
        field (DTYP, "stream")
        field (OUT,  "@test.proto test1 device")
    }
    record (longin, "DZ:test2")
    {
        field (DTYP, "stream")
        field (INP,  "@test.proto test2 device")
    }
    record (longin, "DZ:test3")
    {
        field (DTYP, "stream")
        field (INP,  "@test.proto test3 device")
    }
    record (longin, "DZ:test4")
    {
        field (DTYP, "stream")
        field (INP,  "@test.proto test4 device")
    }
}

set protocol {
    OutTerminator = LF;
    MaxInput = 4;
    test1 {
           out "%r"; out "%.1r"; out "%.2r"; out "%.3r"; out "%.4r"; out "%.5r";
           out "%#r"; out "%#.1r"; out "%#.2r"; out "%#.3r"; out "%#.4r"; out "%#.5r";
           out "%r"; out "%1r"; out "%2r"; out "%3r"; out "%4r"; out "%5r";
           out "%0r"; out "%01r"; out "%02r"; out "%03r"; out "%04r"; out "%05r";
           out "%#0r"; out "%#01r"; out "%#02r"; out "%#03r"; out "%#04r"; out "%#05r";
           };
    test2 {
        in "%04.4r";        
        out "%08x";
    }
    test3{
        in "%#3r\?";        
        out "%08x";
    }
    test4{
        in "%#03r\?";        
        out "%08x";
    }
}

set startup {
}

set debug 0

startioc

put DZ:test1 0
assure "\x00\n"
assure "\x00\n"
assure "\x00\x00\n"
assure "\x00\x00\x00\n"
assure "\x00\x00\x00\x00\n"
assure "\x00\x00\x00\x00\x00\n"
assure "\x00\n"
assure "\x00\n"
assure "\x00\x00\n"
assure "\x00\x00\x00\n"
assure "\x00\x00\x00\x00\n"
assure "\x00\x00\x00\x00\x00\n"
assure "\x00\n"
assure "\x00\n"
assure "\x00\x00\n"
assure "\x00\x00\x00\n"
assure "\x00\x00\x00\x00\n"
assure "\x00\x00\x00\x00\x00\n"
assure "\x00\n"
assure "\x00\n"
assure "\x00\x00\n"
assure "\x00\x00\x00\n"
assure "\x00\x00\x00\x00\n"
assure "\x00\x00\x00\x00\x00\n"
assure "\x00\n"
assure "\x00\n"
assure "\x00\x00\n"
assure "\x00\x00\x00\n"
assure "\x00\x00\x00\x00\n"
assure "\x00\x00\x00\x00\x00\n"

put DZ:test1 1
assure "\x01\n"
assure "\x01\n"
assure "\x00\x01\n"
assure "\x00\x00\x01\n"
assure "\x00\x00\x00\x01\n"
assure "\x00\x00\x00\x00\x01\n"
assure "\x01\n"
assure "\x01\n"
assure "\x01\x00\n"
assure "\x01\x00\x00\n"
assure "\x01\x00\x00\x00\n"
assure "\x01\x00\x00\x00\x00\n"
assure "\x01\n"
assure "\x01\n"
assure "\x00\x01\n"
assure "\x00\x00\x01\n"
assure "\x00\x00\x00\x01\n"
assure "\x00\x00\x00\x00\x01\n"
assure "\x01\n"
assure "\x01\n"
assure "\x00\x01\n"
assure "\x00\x00\x01\n"
assure "\x00\x00\x00\x01\n"
assure "\x00\x00\x00\x00\x01\n"
assure "\x01\n"
assure "\x01\n"
assure "\x01\x00\n"
assure "\x01\x00\x00\n"
assure "\x01\x00\x00\x00\n"
assure "\x01\x00\x00\x00\x00\n"

put DZ:test1 -1
assure "\xff\n"
assure "\xff\n"
assure "\xff\xff\n"
assure "\xff\xff\xff\n"
assure "\xff\xff\xff\xff\n"
assure "\xff\xff\xff\xff\xff\n"
assure "\xff\n"
assure "\xff\n"
assure "\xff\xff\n"
assure "\xff\xff\xff\n"
assure "\xff\xff\xff\xff\n"
assure "\xff\xff\xff\xff\xff\n"
assure "\xff\n"
assure "\xff\n"
assure "\xff\xff\n"
assure "\xff\xff\xff\n"
assure "\xff\xff\xff\xff\n"
assure "\xff\xff\xff\xff\xff\n"
assure "\xff\n"
assure "\xff\n"
assure "\x00\xff\n"
assure "\x00\x00\xff\n"
assure "\x00\x00\x00\xff\n"
assure "\x00\x00\x00\x00\xff\n"
assure "\xff\n"
assure "\xff\n"
assure "\xff\x00\n"
assure "\xff\x00\x00\n"
assure "\xff\x00\x00\x00\n"
assure "\xff\x00\x00\x00\x00\n"

put DZ:test1 269554195
assure "\x13\n"
assure "\x13\n"
assure "\x12\x13\n"
assure "\x11\x12\x13\n"
assure "\x10\x11\x12\x13\n"
assure "\x00\x10\x11\x12\x13\n"
assure "\x13\n"
assure "\x13\n"
assure "\x13\x12\n"
assure "\x13\x12\x11\n"
assure "\x13\x12\x11\x10\n"
assure "\x13\x12\x11\x10\x00\n"
assure "\x13\n"
assure "\x13\n"
assure "\x00\x13\n"
assure "\x00\x00\x13\n"
assure "\x00\x00\x00\x13\n"
assure "\x00\x00\x00\x00\x13\n"
assure "\x13\n"
assure "\x13\n"
assure "\x00\x13\n"
assure "\x00\x00\x13\n"
assure "\x00\x00\x00\x13\n"
assure "\x00\x00\x00\x00\x13\n"
assure "\x13\n"
assure "\x13\n"
assure "\x13\x00\n"
assure "\x13\x00\x00\n"
assure "\x13\x00\x00\x00\n"
assure "\x13\x00\x00\x00\x00\n"

put DZ:test1 -559038737
assure "\xef\n"
assure "\xef\n"
assure "\xbe\xef\n"
assure "\xad\xbe\xef\n"
assure "\xde\xad\xbe\xef\n"
assure "\xff\xde\xad\xbe\xef\n"
assure "\xef\n"
assure "\xef\n"
assure "\xef\xbe\n"
assure "\xef\xbe\xad\n"
assure "\xef\xbe\xad\xde\n"
assure "\xef\xbe\xad\xde\xff\n"
assure "\xef\n"
assure "\xef\n"
assure "\xff\xef\n"
assure "\xff\xff\xef\n"
assure "\xff\xff\xff\xef\n"
assure "\xff\xff\xff\xff\xef\n"
assure "\xef\n"
assure "\xef\n"
assure "\x00\xef\n"
assure "\x00\x00\xef\n"
assure "\x00\x00\x00\xef\n"
assure "\x00\x00\x00\x00\xef\n"
assure "\xef\n"
assure "\xef\n"
assure "\xef\x00\n"
assure "\xef\x00\x00\n"
assure "\xef\x00\x00\x00\n"
assure "\xef\x00\x00\x00\x00\n"

process DZ:test2
send   "\x01\x02\x03\x04"
assure "01020304\n"
process DZ:test2
send   "\xde\xad\xbe\xef"
assure "deadbeef\n"
process DZ:test2
send   "\x00\x00\x00\x00"
assure "00000000\n"

process DZ:test3
send   "\x04\x03\x02\x01"
assure "00020304\n"
process DZ:test3
send   "\xde\xad\xbe\xef"
assure "ffbeadde\n"
process DZ:test3
send   "\x00\x00\x00\x00"
assure "00000000\n"

process DZ:test4
send   "\x04\x03\x02\x01"
assure "00020304\n"
process DZ:test4
send   "\xde\xad\xbe\xef"
assure "00beadde\n"
process DZ:test4
send   "\x00\x00\x00\x00"
assure "00000000\n"


finish
